package com.rq.demo.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.rq.base.util.LOG;
import com.rq.demo.R;

public class VerifySeekBar extends View {
    public VerifySeekBar(Context context) {
        this(context, null, 0);
    }

    public VerifySeekBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VerifySeekBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private Paint paint;
    private int moveX;
    private RectF bgRect;
    private RectF upRect;
    private Bitmap inBitmap;
    //    private Bitmap outBitmap;
    private int bitmapWidth;
    private int bitmapHeight;
    private int defaultBackgroundColor = Color.parseColor("#CCCCCC");
    private int defaultCompleteColor = Color.GREEN;

    //防止多次调用
    private boolean isNewDown = true;
    private boolean isComplete = false;
    private OnSeekbarCompleteListener mOnSeekbarCompleteListener;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(defaultBackgroundColor);
        int rWidth = getHeight() / 2;
        bgRect.set(rWidth, 0, getWidth() - rWidth, getHeight());
        canvas.drawRect(bgRect, paint);
        RectF oval = new RectF(0, 0, getHeight(), getHeight());
        canvas.drawArc(oval, 90, 180, true, paint);
        RectF oval2 = new RectF(getWidth() - getHeight(), 0, getWidth(), getHeight());

        canvas.drawArc(oval2, -90, 180, true, paint);
        canvas.drawRoundRect(bgRect, getHeight() / 2, getHeight() / 2, paint);
        canvas.drawRect(rWidth, 0, getWidth() - rWidth, getHeight(), paint);// 正方形
        if (moveX > getWidth()) {
            moveX = getWidth();
        }
        if (moveX <= 0) {
            moveX = 0;
        }
        if (isComplete) {
            moveX = getWidth();
        }
        int movePadding = getHeight() / 2;
        LOG.e("VerifySeekBar", "79:" + isComplete);
        LOG.e("VerifySeekBar", "80:" + (moveX - bitmapWidth / 2));
        LOG.e("VerifySeekBar", "81:" + (getWidth() - movePadding / 2));

        int r = isComplete ? getWidth() - movePadding : moveX - bitmapWidth / 2;
        int l = moveX == 0 ? 0 : movePadding;
        Log.e("VerifySeekBar", "onDraw.l:" + l);
        Log.e("VerifySeekBar", "onDraw.r:" + r);
        upRect.set(l, 0, r > l ? r : l, getHeight());
        paint.setColor(defaultCompleteColor);
        if (moveX > 0) {
            canvas.drawArc(oval, 90, 180, true, paint);
        }
        if (moveX == getWidth()) {
            canvas.drawArc(oval2, -90, 180, true, paint);
        }
        canvas.drawRect(upRect, paint);
        LOG.e("VerifySeekBar", "93:" + bitmapWidth);

        if (moveX == getWidth()) {
            getParent().requestDisallowInterceptTouchEvent(false);
//            if (outBitmap != null)
//                canvas.drawBitmap(outBitmap, getWidth() - bitmapWidth, 0, paint);
            if (isNewDown) {
                if (mOnSeekbarCompleteListener != null) mOnSeekbarCompleteListener.complete();
                isNewDown = false;
            }
        } else {
            if (inBitmap != null)
                canvas.drawBitmap(inBitmap, Math.max(0, moveX - bitmapWidth), 0, paint);
            if (mOnSeekbarCompleteListener != null) {
                mOnSeekbarCompleteListener.unComplete();
            }
        }
    }

    private void initView() {
        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(defaultCompleteColor);
        paint.setStrokeWidth(2);
        bgRect = new RectF();
        upRect = new RectF();
        Bitmap inBitmap = getBitmap(R.mipmap.icon);
        if (inBitmap != null) {
            this.inBitmap = inBitmap;
            bitmapWidth = inBitmap.getWidth();
            bitmapHeight = inBitmap.getHeight();
            LOG.e("VerifySeekBar", "120:");
            float in = getHeight() / (inBitmap.getHeight() * 1F);
            Matrix matrix = new Matrix();
            // 缩放原图
            matrix.postScale(in, in);
            //bmp.getWidth(), bmp.getHeight()分别表示缩放后的位图宽高
//            Bitmap inC = Bitmap.createBitmap(inBitmap, 0, 0, inBitmap.getWidth(), inBitmap.getHeight(),
//                    matrix, true);
//            if (inC != null) {
//                LOG.e("VerifySeekBar", "137:");
//                this.inBitmap = inC;
//                Bitmap outBitmap = getBitmap(getContext(), R.mipmap.icon);
//                float ou = getHeight() / (outBitmap.getHeight() * 1F);
//                Matrix matrixou = new Matrix();
//                // 缩放原图
//                matrixou.postScale(ou, ou);
//                Bitmap outC = Bitmap.createBitmap(outBitmap, 0, 0, outBitmap.getWidth(), outBitmap.getHeight(),
//                        matrixou, true);
//
//                bitmapWidth = inC.getWidth();
//                bitmapHeight = inC.getHeight();
//                this.outBitmap = outC;
//            }
        }
    }

    private Bitmap getBitmap(int vectorDrawableId) {
        Bitmap bitmap = null;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            Drawable vectorDrawable = getContext().getDrawable(vectorDrawableId);
            bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
                    vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            vectorDrawable.draw(canvas);
        } else {
            bitmap = BitmapFactory.decodeResource(getContext().getResources(), vectorDrawableId);
        }
        return bitmap;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isNewDown = true;
                moveX = bitmapWidth;
            case MotionEvent.ACTION_MOVE:
                moveX = (int) event.getX() + bitmapWidth / 2;
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                if (moveX < getWidth()) {
                    moveX = 0;
                }
                invalidate();
                break;
            default:
                break;
        }
        return true;
    }

    public void setOnSeekbarCompleteListener(OnSeekbarCompleteListener onSeekbarCompleListenner) {
        this.mOnSeekbarCompleteListener = onSeekbarCompleListenner;
    }

    public interface OnSeekbarCompleteListener {
        void complete();

        void unComplete();
    }
}